﻿using UnityEngine;
using System.Collections;
using GDGeek;
using System;
using Fungus;
using CommonIoC;

public static class FirstTankNPCState
{
    public const string Wait_TalkState = "等待对话状态";
    public const string TalkEnd_Up_Tank = "没道理把战车让给你，谈话结束后红狼上车动作";
    public const string DriveCarLeft = "红狼驾驶战车往左";
    public const string Down_Tank = "红狼驾驶战车往左";
    public const string RenLang_WalkUp_OneStep = "红狼往上走一步";
    public const string RenLang_WalkLeft_OneStep = "红狼往左走一步";
    public const string RenLang_WalkUp_TwoStep = "红狼往上走两步";
    public const string RenLang_WalkRight_TwoStep = "红狼往右走两步";
    public const string RenLang_WalkDown_TwoStep = "红狼往下走两步";

    public const string RenLang_WalkLeft_OneStepBackPos = "红狼往左走一步,回到下中位置";

    public const string RenLang_WalkUp_OneStep_SeeFirstTank = "红狼往上走一步,检查第一辆坦克的装备情况";

    public const string RenLang_WalkUp_TwoStep_SeeFirstTank = "红狼往上走两步,检查第一辆坦克的装备情况";


    public const string RenLang_SeeFirstTank_Talk = "红狼检查第一辆坦克的装备情况,说话，没有副炮状态";

    public const string RenLang_WalkDown_TwoStep_UpTank = "红狼往下走两步,准备上车状态";

    public const string RenTank_WalkRight_EndTalkToPlayer = "红狼开车往右走两步,最后一次跟角色对话状态";

    public const string RenTank_WalkDownToAA = "红狼开车往下走到A点";

    public const string RenTank_WalkRightToBB = "红狼开车往下走到B点";

    public const string RenTank_WalkDownToCC = "红狼开车往下走到C点";

    public const string RenTank_WalkRightToDD = "红狼开车往下走到D点";



}
public class FirstTankNPCCtl : MonoBehaviour
{

    public SpriteRenderer redLangRender;
    public SpriteRenderer redTankRender;

    public GameObject red_Lang_Person;
    public GameObject red_Tank_Object;



    private string battle_stage;

    private FSM fsm_ = new FSM();

    public Transform leftdownpos;
    public Transform upzonetankpos;
    public Transform endtalkpos;
    public Transform endpointAAA;
    public Transform endpointBBB;
    public Transform endpointCCC;
    public Transform endpointDDD;

    public Sprite person_LeftSprite;
    public Sprite redtank_LeftSprite;

    public Sprite redtank_RightSprite;

    public Sprite redtank_UpSprite;
    public Sprite redtank_DownSprite;
    public Sprite[] redLangWalkUp;
    public Sprite[] redLangWalkDown;
    [Header("Fungus的Flowchart对象")]
    public Flowchart flowchart;

    // Use this for initialization
    void Start()
    {
        //spriteRenderer = renLangPerson.transform.GetChild(0).GetComponent<SpriteRenderer>();
        //  redLangRender = red_Lang_Person.GetComponent<SpriteRenderer>();
        //  redTankRender = red_Tank_Object.GetComponent<SpriteRenderer>();
        flowchart = GameObject.Find("MainFlowchart").GetComponent<Flowchart>();


        InitFsmState();

       // SetRedPerson_And_TankRenderShow(false, false);
      //  Find_FlowChat();
    }

    void InitFsmState()
    {
        //添加-玩家-操作状态
        fsm_.addState(FirstTankNPCState.Wait_TalkState, OnWait_TalkState());
        fsm_.addState(FirstTankNPCState.DriveCarLeft, OnDriveCarLeft());
        fsm_.addState(FirstTankNPCState.RenLang_WalkUp_TwoStep_SeeFirstTank, OnRenLang_WalkUp_TwoStep_SeeFirstTank());

        fsm_.addState(FirstTankNPCState.RenLang_WalkDown_TwoStep_UpTank, OnRenLang_WalkDown_TwoStep_UpTank());

        fsm_.addState(FirstTankNPCState.RenLang_WalkDown_TwoStep_UpTank, OnRenLang_WalkDown_TwoStep_UpTank());

        fsm_.addState(FirstTankNPCState.RenTank_WalkRight_EndTalkToPlayer, OnRenTank_WalkRight_EndTalkToPlayer());

        fsm_.addState(FirstTankNPCState.RenTank_WalkDownToAA, OnRenTank_WalkDownToAA());

        fsm_.addState(FirstTankNPCState.RenTank_WalkRightToBB, OnRenTank_WalkRightToBB());

        fsm_.addState(FirstTankNPCState.RenTank_WalkDownToCC, OnRenTank_WalkDownToCC());

        fsm_.addState(FirstTankNPCState.RenTank_WalkRightToDD, OnRenTank_WalkRightToDD());



        fsm_.init(FirstTankNPCState.Wait_TalkState);

    }

    private State OnRenTank_WalkRightToDD()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redTankCanMoveToDDD = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        //state.addAction("切换到等待玩家-点击主UI状态", FirstTankTurnState.State_WaitPlayer_ClickMainUI);

        return state;
    }

    private State OnRenTank_WalkDownToCC()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redTankCanMoveToCCC = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换红狼驾车往DDD点移动", FirstTankNPCState.RenTank_WalkRightToDD);

        return state;
    }

    private State OnRenTank_WalkRightToBB()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redTankCanMoveToBBB = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换红狼驾车往CCC点移动", FirstTankNPCState.RenTank_WalkDownToCC);

        return state;
    }

    private State OnRenTank_WalkDownToAA()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redTankCanMoveToAAA = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换红狼驾车往BBB点移动", FirstTankNPCState.RenTank_WalkRightToBB);

        return state;
    }

    private State OnRenTank_WalkRight_EndTalkToPlayer()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redTankCanMoveRightEndTalkToPlayer = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换到红狼往下走，，去往AAA点", FirstTankNPCState.RenTank_WalkDownToAA);

        return state;
    }

    void Find_FlowChat()
    {
        try
        {
            flowchart = GameObject.Find("Flowchart").GetComponent<Flowchart>();
        }
        catch
        {
            Debug.Log("未找到Flowchart对象");
        }
    }


    private State OnRenLang_WalkDown_TwoStep_UpTank()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redLangCanWalkDown = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换到红狼往右走，最后一次和玩家对话状态", FirstTankNPCState.RenTank_WalkRight_EndTalkToPlayer);

        return state;
    }

    private State OnRenLang_WalkUp_TwoStep_SeeFirstTank()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            redLangCanWalkUpSeeTank = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换到红狼往下走，准备回到他自己的战车状态", FirstTankNPCState.RenLang_WalkDown_TwoStep_UpTank);

        return state;
    }

    private State OnDriveCarLeft()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            //Vector3 thispos = 
            //renLangPerson.gameObject.transform.Translate(this.transform.position, leftdownpos);
            redLangRender.enabled = false;
            redLangCanDriveLeft = true;
            battle_stage = state.name;
            Debug.Log("进入" + battle_stage + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };



        state.addAction("切换到红狼下车往上走到firsttank查看状态", FirstTankNPCState.RenLang_WalkUp_TwoStep_SeeFirstTank);

        return state;
    }

    private State OnWait_TalkState()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            SetRedPerson_And_TankRenderShow(false, false);
            battle_stage = state.name;
            Debug.Log("进入"+ battle_stage +"状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + battle_stage + "状态");
        };


       
        state.addAction("切换到红狼驾车往左行驶状态", FirstTankNPCState.DriveCarLeft);

        return state;
    }

    public void SetPlayer_TurnLeft()
    {
        GameObject.Find("GameMainPlayer").GetComponent<SpriteRenderer>().sprite = person_LeftSprite;
    }

    public void SendFSM_RenLangDriveCarLeft()
    {
        Fsm_PostMsg("切换到红狼驾车往左行驶状态");
    }

    public void SendFSM_RenLangWalkUp_TwoStep_SeeFirstTank()
    {
        Fsm_PostMsg("切换到红狼下车往上走到firsttank查看状态");
    }
    public void SendFSM_RenLangWalkDown_TwoStep_BackTank()
    {
        Fsm_PostMsg("切换到红狼往下走，准备回到他自己的战车状态");
    }

    //切换到红狼往右走，最后一次和玩家对话状态
    public void SendFSM_RenLangWalkRight_EndTalkToPlayer()
    {
        Fsm_PostMsg("切换到红狼往右走，最后一次和玩家对话状态");
    }

    public void SendFSM_RedTankMoveToAAA()
    {
        Fsm_PostMsg("切换到红狼往下走，，去往AAA点");
    }

    public void SendFSM_RedTankMoveToBBB()
    {
        Fsm_PostMsg("切换红狼驾车往BBB点移动");
    }
    public void SendFSM_RedTankMoveToCCC()
    {
        Fsm_PostMsg("切换红狼驾车往CCC点移动");
    }

    public void SendFSM_RedTankMoveToDDD()
    {
        Fsm_PostMsg("切换红狼驾车往DDD点移动");
    }


    public void SetRenderShow()
    {
        SetRedPerson_And_TankRenderShow(true, true);

       // redLangRender.enabled = true;
       // redTankRender.enabled = true;
    }

    public void SendFlowChat_SayBadTank()
    {
        flowchart.SendFungusMessage("启动说是一辆破车");
    }

    public void SendFlowChat_SayTankGivePlayer()
    {
        flowchart.SendFungusMessage("启动你来处理那辆旧战车吧");
    }

    public void Fsm_PostMsg(string msg)
    {

        //发送一个消息给状态机
        fsm_.post(msg);
    }

    
    public float framesPerSecond;
    private SpriteRenderer spriteRenderer;

    public bool canAutoPlaySprite = false;
    void AutoPlayRedLangWalkUpSprite()
    {
        if (canAutoPlaySprite)
        {
           
            int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
            index = index % redLangWalkUp.Length;
            //spriteRenderer.sprite = redLangWalkUp[index];
            redLangRender.sprite = redLangWalkUp[index];
            Debug.Log("sssss:" +  index );
        }
    }

    void AutoPlayRedLangWalkDownSprite()
    {
        if (canAutoPlaySprite)
        {
            int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
            index = index % redLangWalkDown.Length;
            //spriteRenderer.sprite = redLangWalkDown[index];
            redLangRender.sprite = redLangWalkDown[index];
        }
    }


    public float moveSpeed;

    bool redLangCanDriveLeft = false;

    bool redLangCanWalkUpSeeTank = false;

    bool redLangCanWalkDown = false;

    bool redTankCanMoveRightEndTalkToPlayer = false;

    bool redTankCanMoveToAAA = false;

    bool redTankCanMoveToBBB = false;

    bool redTankCanMoveToCCC = false;

    bool redTankCanMoveToDDD = false;

    // Update is called once per frame
    void Update()
    {
        MoveAnd_CheckPos();
    }

    void SetRedPerson_MoveToPoint( Vector3 targetpoint) 
    {
        red_Lang_Person.transform.position = Vector3.MoveTowards(red_Lang_Person.transform.position, targetpoint, moveSpeed);
       
    }

    void SetRedTank_MoveToPoint(Vector3 targetpoint)
    {
        red_Tank_Object.transform.position = Vector3.MoveTowards(red_Tank_Object.transform.position, targetpoint, moveSpeed);
    }


    Vector3 GetRedPerson_Pos()
    {
        return red_Lang_Person.transform.position;
    }

    Vector3 GetRedTank_Pos()
    {
        return red_Tank_Object.transform.position;
    }

    void SetRedPerson_And_TankRenderShow(bool p , bool t)
    {
        redLangRender.enabled = p;

        redTankRender.enabled = t;
    }


    int flag = 1;
    void MoveAnd_CheckPos()
    {
        //红狼上车，第一次往左移动逻辑
        RedPerson_FirstDriveLeft();
        //红狼下车，往上走到第一辆坦克，，
        RedPerson_DownTankMoveUp();

        //红狼往下走，返回他的战车位置
        RedPerson_MoveDown_UpTank();

        //红狼驾车往右走，最后一次和玩家对话
        RedTank_MoveRight_preEndTalkToPlayer();

        //对话完，红狼坦克往下走一步
        RedTank_MoveToAAAPoint();

        RedTank_MoveToBBBPoint();

        RedTank_MoveToCCCPoint();

        RedTank_MoveToDDDPoint();
    }

    /// <summary>
    /// 和战斗战后后，红狼和角色对话完毕后，红狼上战车，往左移动逻辑
    /// </summary>
    void RedPerson_FirstDriveLeft()
    {
        if (redLangCanDriveLeft)
        {

            SetRedPerson_And_TankRenderShow(false, true);
            redTankRender.sprite = redtank_LeftSprite;
            SetRedPerson_MoveToPoint( leftdownpos.position );
            SetRedTank_MoveToPoint(leftdownpos.position);
          
        }
        if ( GetRedTank_Pos()  == leftdownpos.position)
        {
            Debug.Log("到达Leftdown点，，准备转换为，向上走状态");
            redLangCanDriveLeft = false;
            if (flag == 1)
            {
                flag++;
                //发送FSM转换状态
                SendFSM_RenLangWalkUp_TwoStep_SeeFirstTank();
            }
        }

    }

    /// <summary>
    /// 红狼下车，人往上走，查看第一辆坦克逻辑
    /// </summary>
    void RedPerson_DownTankMoveUp()
    {
        if (redLangCanWalkUpSeeTank)
        {
            SetRedPerson_And_TankRenderShow(true, true);
            canAutoPlaySprite = true;
            AutoPlayRedLangWalkUpSprite();
            SetRedPerson_MoveToPoint(upzonetankpos.position);
          //  renLangPerson.transform.position = Vector3.MoveTowards(renLangPerson.transform.position, upzonetankpos.position, moveSpeed);
        }
        if (GetRedPerson_Pos() == upzonetankpos.position)
        {
            Debug.Log("到达upzonetankpos点，，准备转换为，说话，，这是辆破车状态");
            redLangCanWalkUpSeeTank = false;
            canAutoPlaySprite = false;
            if (flag == 2)
            {
                flag++;
                SendFlowChat_SayBadTank();
            }
        }
    }

    /// <summary>
    /// 红狼说破车，往下走，准备上坦克逻辑
    /// </summary>
    void RedPerson_MoveDown_UpTank()
    {
        if (redLangCanWalkDown)
        {

            canAutoPlaySprite = true;
            AutoPlayRedLangWalkDownSprite();
            SetRedPerson_MoveToPoint(leftdownpos.position);
           
        }
        if (GetRedPerson_Pos() == leftdownpos.position)
        {
            Debug.Log("到达leftdownpos点，，准备转换为，先上车，红狼坦克往右移动状态");
            redLangCanWalkDown = false;
            canAutoPlaySprite = false;

            SetRedPerson_And_TankRenderShow(false, true);
            redTankRender.sprite = redtank_RightSprite;
            

            SendFSM_RenLangWalkRight_EndTalkToPlayer();
           // RenLangWalkRight_EndTalkToPlayer()
            // SendFlowChat_SayBadTank();
        }
    }

    /// <summary>
    /// 红狼驾车往右走 ，，最后一次和玩家对话
    /// </summary>
    void RedTank_MoveRight_preEndTalkToPlayer()
    {
        if (redTankCanMoveRightEndTalkToPlayer)
        {
            SetRedTank_MoveToPoint(endtalkpos.position);

            redTankRender.sprite = redtank_RightSprite;
            
        }
        if( GetRedTank_Pos() == endtalkpos.position)
        {
            redTankCanMoveRightEndTalkToPlayer = false;
            red_Lang_Person.transform.position = endtalkpos.position;

            SetRedPerson_And_TankRenderShow(true, true);

            if(flag== 3)
            {
                flag++;
                SendFlowChat_SayTankGivePlayer();
            }
        }
    }

    void RedTank_MoveToAAAPoint()
    {
        if (redTankCanMoveToAAA)
        {
            SetRedPerson_And_TankRenderShow(false, true);
            redTankRender.sprite = redtank_DownSprite;
            SetRedTank_MoveToPoint(endpointAAA.position);
        }
        if(GetRedTank_Pos() == endpointAAA.position)
        {
            redTankCanMoveToAAA = false;
            if(flag== 4)
            {
                flag++;
                SendFSM_RedTankMoveToBBB();
            }
            //下一步操作
        }

    }

    void RedTank_MoveToBBBPoint()
    {
        if (redTankCanMoveToBBB)
        {
            redTankRender.sprite = redtank_RightSprite;
            SetRedTank_MoveToPoint(endpointBBB.position);
        }

        if (GetRedTank_Pos() == endpointBBB.position)
        {
            redTankCanMoveToBBB = false;
            if (flag == 5)
            {
                flag++;
                SendFSM_RedTankMoveToCCC();
            }
            //下一步操作
        }

    }

    void RedTank_MoveToCCCPoint()
    {
        if (redTankCanMoveToCCC)
        {
            redTankRender.sprite = redtank_DownSprite;
            SetRedTank_MoveToPoint(endpointCCC.position);
        }

        if (GetRedTank_Pos() == endpointCCC.position)
        {
            redTankCanMoveToCCC = false;
            if (flag == 6)
            {
                flag++;
                SendFSM_RedTankMoveToDDD();
            }
            //下一步操作
        }

    }


    void RedTank_MoveToDDDPoint()
    {
        if (redTankCanMoveToDDD)
        {
            redTankRender.sprite = redtank_RightSprite;
            SetRedTank_MoveToPoint(endpointDDD.position);
        }

        if (GetRedTank_Pos() == endpointDDD.position)
        {
            redTankCanMoveToDDD = false;
            if (flag == 7)
            {
                flag++;
                SetRedPerson_And_TankRenderShow(false, false);

                SendTalkIs_AllEnd_Event();
               // SendFSM_RedTankMoveToDDD();
            }
            //下一步操作
        }

    }

    public void SendTalkIs_AllEnd_Event()
    {
        // PlayerGameStage stage = PlayerGameStage.CanMove_And_WaitClickTopUI;
        //  EventMsgHelper.SendEvent(GameEventString.OnTalkIsEnd, this, GameHashKeyString.TalkIsEndKey, stage);
        GameObject.Find("MainFlowchart").GetComponent<FlowchartView>().IsEndTalk();


    }

    public void DestoryTank_1_Object()
    {
        try
        {
            GameObject.Find("tank_001").GetComponent<TriggerGetTank>().DestoryThisObject();
        }
        catch
        {
            Debug.LogError("Tank001第一辆坦克对象已经被摧毁，没有找到，，");
        }
    }


}
